package com.vpx.server.utils;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import com.sun.management.OperatingSystemMXBean;
/**
 * 获取磁盘空间 内存信息
 */
public class DiskUtils {
    public static void main(String[] args) throws IOException {
        getDiskInfo();
        getMemoryInfo();
    }
    /**
     * 获取系统各个硬盘的总容量、已经使用的容量、剩余容量和使用率
     * @throws IOException
     */
    public static Map<String,String> getDiskInfo() throws IOException {
        DecimalFormat df = new DecimalFormat("#0.00");
        File[] disks = File.listRoots();
        long allTotalSpace = 0;
        long allUsableSpace = 0;
        long allFreeSpace = 0;
        Map<String,String> diskInfo = new HashMap<>();
        for (File file : disks) {
            // 获取盘符
            System.out.print(file.getCanonicalPath() + "   ");
            // 获取总容量
            long totalSpace = file.getTotalSpace();
            // 获取剩余容量
            long  freeSpace = file.getUsableSpace();
            // 获取已经使用的容量
            long usableSpace = totalSpace - freeSpace;

            allTotalSpace = allTotalSpace + totalSpace;
            allUsableSpace = allUsableSpace + usableSpace;
            allFreeSpace = allFreeSpace + allFreeSpace;
//            System.out.print("总容量： " + transformation(totalSpace));
//            System.out.print("已经使用： " + transformation(freeSpace));
//            System.out.print("剩余容量： " + transformation(usableSpace));
//            System.out.println("使用率： " + Double.parseDouble(df.format(useRate)) + "%   ");
        }
        diskInfo.put("allTotalSpace",transformation(allTotalSpace));
        diskInfo.put("allUsableSpace",transformation(allUsableSpace));
        diskInfo.put("allFreeSpace",(allTotalSpace-allUsableSpace)+"");
        return diskInfo;
    }
    /**
     * 获取内存使用情况
     */
    public static void getMemoryInfo() {
        OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 获取内存总容量
        long totalMemorySize = mem.getTotalPhysicalMemorySize();
        // 获取可用内存容量
        long freeMemorySize = mem.getFreePhysicalMemorySize();
        System.out.println("内存总容量：" + transformation(totalMemorySize) );
        System.out.println("可用容量：" + transformation(freeMemorySize));
    }
    /**
     * 将字节容量转化为GB
     */
    public static String transformation(long size){
        return size / 1024 / 1024 / 1024 + "GB"+"   ";
    }
}
